package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.dict.constant.DicConstants;
import org.dromara.system.dict.support.DictSupport;
import org.dromara.system.domain.AlarmIndicator;
import org.dromara.system.domain.bo.AlarmIndicatorBo;
import org.dromara.system.domain.vo.AlarmIndicatorVo;
import org.dromara.system.mapper.AlarmIndicatorMapper;
import org.dromara.system.service.IAlarmIndicatorService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 监测点位Service业务层处理
 *
 * @author Lion Li
 * @date 2025-10-27
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AlarmIndicatorServiceImpl implements IAlarmIndicatorService {

    private final AlarmIndicatorMapper baseMapper;

    private final DictSupport dictSupport;

    /**
     * 查询监测点位
     *
     * @param id 主键
     * @return 监测点位
     */
    @Override
    public AlarmIndicatorVo queryById(Long id){
        AlarmIndicatorVo alarmIndicatorVo = baseMapper.selectVoById(id);
        if (alarmIndicatorVo != null) {
            // 监测类型
            Map<String, String> indicatorTypeMap = dictSupport.nameMap(DicConstants.TYPE_INDICATOR_TYPE);
            alarmIndicatorVo.setIndicatorTypeName(indicatorTypeMap.getOrDefault(alarmIndicatorVo.getIndicatorType(), ""));
            return alarmIndicatorVo;
        }
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询监测点位列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 监测点位分页列表
     */
    @Override
    public TableDataInfo<AlarmIndicatorVo> queryPageList(AlarmIndicatorBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AlarmIndicator> lqw = buildQueryWrapper(bo);
        Page<AlarmIndicatorVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        if (!CollectionUtils.isEmpty(result.getRecords())) {
            // 监测类型
            Map<String, String> indicatorTypeMap = dictSupport.nameMap(DicConstants.TYPE_INDICATOR_TYPE);
            result.getRecords().forEach(item -> {
                item.setIndicatorTypeName(indicatorTypeMap.getOrDefault(item.getIndicatorType(), ""));
            });
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的监测点位列表
     *
     * @param bo 查询条件
     * @return 监测点位列表
     */
    @Override
    public List<AlarmIndicatorVo> queryList(AlarmIndicatorBo bo) {
        LambdaQueryWrapper<AlarmIndicator> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AlarmIndicator> buildQueryWrapper(AlarmIndicatorBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AlarmIndicator> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AlarmIndicator::getId);
        lqw.like(StringUtils.isNotBlank(bo.getName()), AlarmIndicator::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getSysNum()), AlarmIndicator::getSysNum, bo.getSysNum());
        lqw.eq(StringUtils.isNotBlank(bo.getIndicatorType()), AlarmIndicator::getIndicatorType, bo.getIndicatorType());
        lqw.eq(bo.getRangeTop() != null, AlarmIndicator::getRangeTop, bo.getRangeTop());
        lqw.eq(bo.getRangeBottom() != null, AlarmIndicator::getRangeBottom, bo.getRangeBottom());
        lqw.eq(StringUtils.isNotBlank(bo.getCollectorId()), AlarmIndicator::getCollectorId, bo.getCollectorId());
        lqw.eq(bo.getLowAlarm() != null, AlarmIndicator::getLowAlarm, bo.getLowAlarm());
        lqw.eq(bo.getLowLowAlarm() != null, AlarmIndicator::getLowLowAlarm, bo.getLowLowAlarm());
        lqw.eq(bo.getHighAlarm() != null, AlarmIndicator::getHighAlarm, bo.getHighAlarm());
        lqw.eq(bo.getHighHighAlarm() != null, AlarmIndicator::getHighHighAlarm, bo.getHighHighAlarm());
        lqw.eq(bo.getRealValue() != null, AlarmIndicator::getRealValue, bo.getRealValue());
        lqw.eq(bo.getControlSystem() != null, AlarmIndicator::getControlSystem, bo.getControlSystem());
        lqw.eq(bo.getAlarmType() != null, AlarmIndicator::getAlarmType, bo.getAlarmType());
        lqw.like(StringUtils.isNotBlank(bo.getTagDescription()), AlarmIndicator::getTagDescription, bo.getTagDescription());
        return lqw;
    }

    /**
     * 新增监测点位
     *
     * @param bo 监测点位
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AlarmIndicatorBo bo) {
        AlarmIndicator add = MapstructUtils.convert(bo, AlarmIndicator.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改监测点位
     *
     * @param bo 监测点位
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AlarmIndicatorBo bo) {
        AlarmIndicator update = MapstructUtils.convert(bo, AlarmIndicator.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AlarmIndicator entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除监测点位信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public List<String> getIndicatorNums() {
        List<AlarmIndicator> alarmIndicators = baseMapper.selectList();
        return alarmIndicators.stream().map(AlarmIndicator::getSysNum).toList();
    }
}
